﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class TextboxControl : UserControl
    {
        public string message = "";

        public int maxInput = 50;

        private SpriteFont font;

        /// <summary>
        /// If the message should be sent somewhere, set to true
        /// </summary>
        public bool sendMessage = false;

                /// <summary>
        /// Constructs button object
        /// </summary>
        public TextboxControl()
            : base()
        {

        }

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureTextboxRegular);
            font = TacticsGame.Instance.Content.Load<SpriteFont>(Constants.Font);

            Vector2 fontSize = font.MeasureString(" ");
            width = 100;
            height = (int)fontSize.Y;
            isDraggable = true;
        }
        private void readKeyboardInput()
        {
            char readChar;
            //HACK
            for (int i = 0; i < InputMgr.Instance.KeyboardBuffer.Count; i++)
            {
                readChar = InputMgr.Instance.KeyboardBuffer[i];
                switch (readChar)
                {
                    case '\b': //backspace
                        if (message.Length > 0)
                            message = message.Remove(message.Length - 1);
                        break;
                    case '\r': //return
                         sendMessage = true;
                        break;
                    case '\t': //tab
                        break;
                    default:
                        if (font.Characters.Contains(readChar))
                        {
                            Vector2 size = font.MeasureString(message + readChar);
                            if (size.X <= width && message.Length + 1 < maxInput)                            
                            message += readChar;
                            //message = Util.splitString(message, width);
                        }
                        break;
                }

            }
        }

        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
                        
            if (Selected)
                readKeyboardInput();
        }

        public override void Draw(GameTime gameTime)
        {
            
            Vector2 size;
            if (message != "")
            {
                size = font.MeasureString(message);
            }
            else
            {
                size = font.MeasureString(" ");
            }

            int yPos = Util.getCentreDrawPointY(y, height, (int)size.Y);

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, 
                                                    BlendState.AlphaBlend, 
                                                    SamplerState.PointClamp, 
                                                    DepthStencilState.Default, 
                                                    RasterizerState.CullNone);

            Rectangle targetRect = new Rectangle(x, y, width, height);
            if (Selected)
            {
                TacticsGame.Instance.spriteBatch.Draw(_bgTexture, 
                                                        targetRect, 
                                                        null,
                                                        Color.Gray * opacity,
                                                        0, 
                                                        Vector2.Zero, 
                                                        SpriteEffects.None, 
                                                        0);

                //Blinking Cursor
                if ((gameTime.TotalGameTime.TotalMilliseconds % 1000) < 500)
                    TacticsGame.Instance.spriteBatch.DrawString(font, 
                                                                "|", 
                                                                new Vector2(x + (message.Length > 0 ? size.X : 0) + Constants.ControlSmallOffsets, yPos),
                                                                Color.Black * opacity, 
                                                                0.0f, 
                                                                new Vector2(0, 0), 
                                                                Constants.BlinkerScale, 
                                                                SpriteEffects.None, 
                                                                0);
            }
            else
            {
                TacticsGame.Instance.spriteBatch.Draw(_bgTexture, 
                                                        targetRect,
                                                        null,
                                                        Color.White * opacity, 
                                                        0, 
                                                        Vector2.Zero, 
                                                        SpriteEffects.None, 
                                                        0);                
            }

            TacticsGame.Instance.spriteBatch.DrawString(font, 
                                                        message,
                                                        new Vector2(x + Constants.ControlSmallOffsets , yPos),
                                                        Color.Black * opacity, 
                                                        0.0f, 
                                                        new Vector2(0, 0), 
                                                        Constants.FontScale, 
                                                        SpriteEffects.None, 
                                                        0);

            TacticsGame.Instance.spriteBatch.End();
        }

    }
}
